﻿#include "cd_list.h"
#include "CList.h"
#include "CMap.h"
#include "CQue.h"

#include <string.h>
#include <stdio.h>
#pragma warning(disable:4996)


typedef struct {
    char c;
    int i;
    char* pc ;
    unsigned short us;
} TEST_TYPE;

int test_cmp_fun(TEST_TYPE* p1, TEST_TYPE* p2) {
    return ((p1->c == p2->c) && 0 == strcmp(p1->pc, p2->pc));
}

void cdlist_test1() {
	TEST_TYPE* ph = 0; //声明一个存储类型的指针作为链表头
	TEST_TYPE Data1 = {'A', 5, 0, 65534};
	TEST_TYPE Data2 = {'B', 65538, 0, 108};
	TEST_TYPE* pIt = 0;

	Data1.pc = strdup("Hello");
	Data2.pc = strdup("World");

	ph = cd_list_create_head(TEST_TYPE);
	cd_list_add_item(ph, &Data1);
	cd_list_add_item(ph, &Data2);

	cd_list_for_each_item(ph, &pIt, TEST_TYPE);
		printf("c = %c, i = %d, pc = %s, us = %d\n", pIt->c, pIt->i, pIt->pc, pIt->us);
	cd_list_for_each_item_end;

	cd_list_delete_item(ph, &Data2, TEST_TYPE, test_cmp_fun);
    

	printf("after remove\n");
	cd_list_for_each_item(ph, &pIt, TEST_TYPE);
		printf("c = %c, i = %d, pc = %s, us = %d\n", pIt->c, pIt->i, pIt->pc, pIt->us);
	cd_list_for_each_item_end;

	cd_list_release(ph);
	free(Data1.pc);
	free(Data2.pc);
}

int str_cmp(char** p1, char** p2) {
	return 0 == strcmp(*p1, *p2);
}
void cdlist_test2() {
	char** ph = 0;
	char* pstr1 = 0;
	char* pstr2 = 0;
	char** pit = 0;

	pstr1 = strdup("Good");
	pstr2 = strdup("Morning");

	ph = cd_list_create_head(char*);
	cd_list_add_item(ph, &pstr1);
	cd_list_add_item(ph, &pstr2);

	cd_list_for_each_item(ph, &pit, char*);
		printf("%s\n", *pit);
	cd_list_for_each_item_end;

	cd_list_delete_item(ph, &pstr1, char*, str_cmp);

	printf("after remove-----\n");
	cd_list_for_each_item(ph, &pit, char*);
		printf("%s\n", *pit);
	cd_list_for_each_item_end;
    free(pstr1);
    free(pstr2);
}


void clist_test1() {
	CList l;
	CListIterator it;

	TEST_TYPE Data1 = { 'A', 5, 0, 65534 };
	TEST_TYPE Data2 = { 'B', 65538, 0, 108 };
	Data1.pc = strdup("Hello");
	Data2.pc = strdup("World");


	construct_clist(&l, sizeof(TEST_TYPE));
	construct_clist_iterator(&it);

	l.insert(&l, &Data1);
	l.insert(&l, &Data2);

	printf("clist_test1--------------------------\n");
	for (it.ptr = l.begin(&l); it.ptr != l.end(&l); it.next(&it)) {
		TEST_TYPE* pIt = IT_PTR(it, TEST_TYPE);
		printf("c = %c, i = %d, pc = %s, us = %d\n", pIt->c, pIt->i, pIt->pc, pIt->us);
		if (0 == strcmp("World", pIt->pc)) {
			l.erase(&l, &it);
		}
	}

	printf("~~~~after remove item1~~~~~\n");
	for (it.ptr = l.begin(&l); it.ptr != l.end(&l); it.next(&it)) {
		TEST_TYPE* pIt = IT_PTR(it, TEST_TYPE);
		printf("c = %c, i = %d, pc = %s, us = %d\n", pIt->c, pIt->i, pIt->pc, pIt->us);
	}
	l.release(&l);
	free(Data1.pc);
	free(Data2.pc);
}

void cmap_test1() {
	CMap m;
	char* str = strdup("tomorrow is other day.");
	char* str2 = strdup("asita is other day.");
	char* tmp = 0;

	construct_map(&m, 0);

	m.insert(&m, (void*)12, str);
	tmp = (char*)m.find(&m, (void*)12);
	if(tmp) {
		printf("m[12]=%s\n", tmp);
	}

	m.insert(&m, (void*)12, str2);
	tmp = (char*)m.find(&m, (void*)12);
	if(tmp) {
		printf("m[12]=%s\n", tmp);
	}
    free(str);
    free(str2);
	m.release(&m);
}

void cque_test1() {
	CQue q;
	int tmp = 1;

	construct_queue(&q, 0);

	q.push(&q, (void*)1);
	tmp = 2;
	q.push(&q, (void*)2);
	
	tmp = (int)q.pop(&q);
	printf("pop value=%d\n", tmp);

	tmp = (int)q.pop(&q);
	printf("pop value=%d\n", tmp);

	tmp = (int)q.pop(&q);
	printf("pop value=%d\n", tmp);
	q.list.release(&q.list);
}

int main() {
	cdlist_test1();
	cdlist_test2();

	clist_test1();
	cmap_test1();
	cque_test1();
    return 0;
}
